package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.dto.CreateClueTrackRecordDTO;
import com.huike.clues.domain.vo.ClueTrackListVO;
import com.huike.clues.domain.vo.PageClueVO;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.TbClueTrackRecordService;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.common.constant.ErrorMessageConstants;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 93238
 * @description 针对表【tb_clue_track_record(线索跟进记录)】的数据库操作Service实现
 * @createDate 2023-10-12 06:35:46
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class TbClueTrackRecordServiceImpl extends ServiceImpl<TbClueTrackRecordMapper, TbClueTrackRecord> implements TbClueTrackRecordService {

    private final TbClueTrackRecordMapper mapper;

    private final TbClueMapper tbClueMapper;

    /**
     * 新增线索跟进记录
     */
    @Override
    public void create(CreateClueTrackRecordDTO dto) {
        tbClueMapper.updateStatusByClueId(dto.getClueId());
        if (!save(TbClueTrackRecord.builder()
                .id(dto.getId())
                .clueId(dto.getClueId().toString())
                .createBy(SecurityUtils.getUsername())
                .subject(dto.getSubject())
                .record(dto.getRecord())
                .level(dto.getLevel())
                .createTime(new Date())
                .type("0")
                .nextTime(dto.getNextTime())
                .build())) {
            throw new BaseException(ErrorMessageConstants.FAILED_TO_ADD_CLUE_TRACK_RECORD);
        }
        tbClueMapper.updateInfo(TbClue.builder()
                .id(dto.getClueId())
                .subject(dto.getSubject())
                .level(dto.getLevel())
                .age(dto.getAge())
                .qq(dto.getQq())
                .weixin(dto.getWeixin())
                .build());
    }

    /**
     * 查询线索跟进记录列表
     *
     * @param clueId 线索id
     * @return 返回跟进记录集合
     */
    @Override
    public TableDataInfo<List<ClueTrackListVO>> getClueTracklist(Long clueId) {
        List<ClueTrackListVO> list = mapper.getClueTrackList(clueId);
        TableDataInfo<List<ClueTrackListVO>> res = new TableDataInfo<>(list, list.size());
        res.setCode(200);
        res.setMsg("查询成功");
        return res;
    }

    /**
     * 获取线索跟进记录详细信息
     *
     * @param id 线索id
     * @return 返回详情信息
     */
    @Override
    public PageClueVO getInfoById(Long id) {
        PageClueVO res = mapper.getInfoById(id);
        if (res != null) {
            TbActivity info = tbClueMapper.getActivityInfoByClueId(res.getId());
            if (info != null) {
                res.setActivityName(info.getName());
                res.setActivityInfo(info.getInfo());
            }
        }
        log.info("Get res: {}", res);
        return res;
    }

}




